﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Portal.Exam.Model;
using Portal.Web.Model;
using Portal.Exam.Bussiness;
using Portal.Web.Utility;
using Portal.Core;
using Portal.Systems.Model;

namespace Portal.Exam.Render
{
    public class ExamUltility
    {
        public const string StudentNotAnswer = "Không có câu trả lời!";
        public const string ExamDoing = "Đang thi";
        public const string ExamDone = "Thi xong";
        public const string ExamWaiting = "Chưa thi";
        public const string AccessDenied = "Truy cập bị từ chối!";
        public const string NotAnswer = "Không có câu trả lời";
        public const string CorrectAnswer = "Trả lời đúng";
        public const string WrongAnswer = "Trả lời sai";
        public const int StartRowExcel = 10;

        [AjaxPro.AjaxMethod(AjaxPro.HttpSessionStateRequirement.ReadWrite)]
        public static AjaxOut DrawTreeView(string SiteId)
        {
            AjaxOut OAjaxOut = new AjaxOut();
            try
            {
                AccountTypeCls[] AccountTypes = SystemBussinessUtility.CreateSystemsBussinessProcess(SiteId).ReadingAccountType(SiteId, null);
                string Html =
                              "<script language=javascript>\r\n" +

                              "</script>\r\n" +
                              "<div class=TreeHolder>\r\n" +
                              "<div id=\"tree\">\r\n" +
                              "<ul>\r\n" +
                              " <li class=\"folder expanded\"><a href=\"root\">Danh sách nhóm</a>\r\n" +
                              "     <ul>\r\n";
                for (int iIndex = 0; iIndex < AccountTypes.Length; iIndex++)
                {
                    Html +=
                            "<li class=\"folder expanded\"><a href=\"" + AccountTypes[iIndex].AccountType + "\" target=\"_self\">" + AccountTypes[iIndex].Description + " (" + AccountTypes[iIndex].TotalRole.ToString("#,##0") + ")</a>\r\n" +
                                BuildSubTreeView(SiteId, AccountTypes[iIndex].AccountType).HtmlContent +
                            "</li>\r\n";
                }

                Html += "</ul>\r\n" +
                        "</li>\r\n" +
                        "</ul>\r\n" +
                        "</div>\r\n" +
                        "</div>\r\n";
                OAjaxOut.HtmlContent = Html;
            }
            catch (Exception ex)
            {
                OAjaxOut.Error = true;
                OAjaxOut.InfoMessage = ex.Message.ToString();
                OAjaxOut.HtmlContent = ex.Message.ToString();
            }
            return OAjaxOut;
        }

        [AjaxPro.AjaxMethod(AjaxPro.HttpSessionStateRequirement.ReadWrite)]
        public static AjaxOut DrawTreeViewByAccountType(string SiteId, string UserId)
        {
            AjaxOut OAjaxOut = new AjaxOut();
            try
            {
                

                AccountTypeCls[] AccountTypes = new AccountTypeCls[0];                
                if (WebSessionUtility.GetCurrentLoginUser().IsSystemAdmin)
                {
                    AccountTypes = SystemBussinessUtility.CreateSystemsBussinessProcess(SiteId).ReadingAccountType(SiteId, null);
                }
                else
                {
                    AccountTypes = DoExamBll.ReadingAccountTypeForUser(SiteId, UserId);
                }

                string Html =
                              "<script language=javascript>\r\n" +

                              "</script>\r\n" +
                              "<div class=TreeHolder>\r\n" +
                              "<div id=\"tree\">\r\n" +
                              "<ul>\r\n" +
                              " <li class=\"folder expanded\"><a href=\"root\">Danh sách nhóm</a>\r\n" +
                              "     <ul>\r\n";
                for (int iIndex = 0; iIndex < AccountTypes.Length; iIndex++)
                {
                    Html +=
                            "<li class=\"folder expanded\"><a href=\"" + AccountTypes[iIndex].AccountType + "\" target=\"_self\">" + AccountTypes[iIndex].Description + " (" + AccountTypes[iIndex].TotalRole.ToString("#,##0") + ")</a>\r\n" +
                                BuildSubTreeView(SiteId, AccountTypes[iIndex].AccountType).HtmlContent +
                            "</li>\r\n";
                }

                Html += "</ul>\r\n" +
                        "</li>\r\n" +
                        "</ul>\r\n" +
                        "</div>\r\n" +
                        "</div>\r\n";
                OAjaxOut.HtmlContent = Html;
            }
            catch (Exception ex)
            {
                OAjaxOut.Error = true;
                OAjaxOut.InfoMessage = ex.Message.ToString();
                OAjaxOut.HtmlContent = ex.Message.ToString();
            }
            return OAjaxOut;
        }

        [AjaxPro.AjaxMethod(AjaxPro.HttpSessionStateRequirement.ReadWrite)]
        public static AjaxOut BuildSubTreeView(string SiteId, string ParentAccountType)
        {
            AjaxOut OAjaxOut = new AjaxOut();
            try
            {
                string Html = "";
                AccountTypeCls[] AccountTypes = SystemBussinessUtility.CreateSystemsBussinessProcess(SiteId).ReadingAccountType(SiteId, ParentAccountType);
                Html +=
                    "<ul>\r\n";
                for (int iIndex = 0; iIndex < AccountTypes.Length; iIndex++)
                {
                    Html +=
                            "<li class=\"folder expanded\"><a target=\"_self\" href=\"" + AccountTypes[iIndex].AccountType + "\">" + AccountTypes[iIndex].Description + "</a>\r\n" +
                                BuildSubTreeView(SiteId, AccountTypes[iIndex].AccountType).HtmlContent +
                            "</li>\r\n";
                }

                Html += "</ul>\r\n";
                OAjaxOut.HtmlContent = Html;
            }
            catch (Exception ex)
            {
                OAjaxOut.Error = true;
                OAjaxOut.InfoMessage = ex.Message.ToString();
                OAjaxOut.HtmlContent = ex.Message.ToString();
            }
            return OAjaxOut;
        }
    }
}
